// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

import iView from 'iview'
import 'iview/dist/styles/iview.css'

import axios from 'axios'



Vue.prototype.axios=axios;
//axios.defaults.withCredentials=true; //每次ajax请求时携带cookie
axios.interceptors.response.use(
  response=>{
    if(response.headers.token){
      window.sessionStorage.setItem("token",response.headers.token)
      axios.defaults.headers.Token = response.headers.token;
    }
    return response;
  },error => {
    iView.Message.error('通讯异常')
  }
)
//Header携带Token
axios.defaults.headers.Token=window.sessionStorage.getItem("token")


import VueCookies from 'vue-cookies'
Vue.use(VueCookies)

Vue.use(iView)

Vue.config.productionTip = false

import {store} from "@/store/index"

import {login} from '@/utils/service'
if(!store.state.user){
  //iView.Message.info("当前登录状态：未登录")
  let rememberMe = VueCookies.get("rememberMe");
  if(rememberMe){
    //利用这个登录信息去登录（自动登录）
    login(rememberMe.username,rememberMe.password,data=>{
      if(data.code==100){
        iView.Message.success('自动登录成功');
        window.sessionStorage.setItem('user', JSON.stringify(data.user))
        store.commit("setUser",data.user)
      }else{
        iView.Message.warning("自动登录结果："+data.msg);
      }
    })
  }
}

router.beforeEach((to, from, next) => {
  iView.LoadingBar.start();
  setTimeout(() => {

  },10)
  next();
});

router.afterEach(route => {
  iView.LoadingBar.finish();
});

/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  router,
  render: h => h(App)
})
